//
//  YCHadRecruitViewController.m
//  YCSchool
//
//  Created by gmall on 16/5/24.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  已招募

#import "YCHadRecruitViewController.h"
#import "YCRecruitCell.h"

@interface YCHadRecruitViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,weak) UILabel *numLabel;
@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) NSString *recruitCountString;//招募人数
@property (nonatomic, strong) NSString *addtime;//分页时间
@end

@implementation YCHadRecruitViewController


-(void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"招募学员";
    [self yc_addBackBarButtonItem];
    [self configView];
    
    YCWeakSelf;
    self.tableView.mj_header = [YCRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf loadData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}
-(void)configView
{
    CGFloat navH = 64;
    CGFloat IPHONE_HEIGHT = [UIScreen mainScreen].bounds.size.height;
    CGFloat IPHONE_WIDTH = [UIScreen mainScreen].bounds.size.width;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT)];
    
    
    //header
    UIView *view = [[UIView alloc] initWithSize:CGSizeMake(IPHONE_WIDTH, 100)];
    [view setPosition:CGPointZero atAnchorPoint:CGPointZero];
    self.tableView.tableHeaderView = view;
    
    
    //desc
    UILabel *descText = [[UILabel alloc] initWithSize:CGSizeMake(IPHONE_WIDTH - 25, 13)];
    [descText setPosition:CGPointMake(25, 10) atAnchorPoint:CGPointZero];
    [view addSubview:descText];
    descText.font = [UIFont systemFontOfSize:13];
    descText.textColor = [UIColor redColor];
    descText.text = @"已招募学员";
    
    
    //num
    NSString *text = @"296";
    CGSize size = [text yc_sizeWithFont:[UIFont systemFontOfSize:35] MaxSize:CGSizeMake(MAXFLOAT, 100)];
    
    UILabel *numText = [[UILabel alloc] initWithSize:CGSizeMake(size.width+10, view.yc_height - CGRectGetMaxY(descText.frame))];
    [numText setPosition:CGPointMake(descText.yc_x, CGRectGetMaxY(descText.frame)) atAnchorPoint:CGPointZero];
    [view addSubview:numText];
    numText.font = [UIFont systemFontOfSize:35];
    numText.textColor = [UIColor colorWithRed:0 green:180 blue:225 alpha:1];
    numText.text = text;
    self.numLabel = numText;
    
    //person
    UILabel *p = [[UILabel alloc] initWithSize:CGSizeMake(100, numText.yc_height)];
    [p setPosition:CGPointMake(CGRectGetMaxX(numText.frame)+ 3 , CGRectGetMaxY(numText.frame)) atAnchorPoint:CGPointMake(0, 1)];
    [view addSubview:p];
    p.textColor = numText.textColor;
    p.font = [UIFont systemFontOfSize:12];
    p.text = @"人";
    
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:245 green:245 blue:245 alpha:1];
    
}

#pragma mark - 数据处理

- (void)loadData
{
    NSDictionary *params = @{
                             @"uid":@"1",
                             @"token":accessToken_Test,
                             };
    [[YCNetworManager shareManager] postWithUrl:YCAPIRecruitList params:params success:^(NSURLSessionDataTask *task, ServiceResponse *response) {
        self.recruitCountString = [NSString stringWithFormat:@"%zd",((NSNumber *)response.data[@"rows"]).intValue];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.numLabel.text = self.recruitCountString;
        });
        self.dataArray = [YCHadRecruitModel yc_objectArrayWithKeyValuesArray:response.data[@"list"]];
        //        //addtime进行分页
        //        YCHadRecruitModel *model = self.dataArray.lastObject;
        //        self.addtime = model.addtime;
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        YCLog(@"%@",error.localizedDescription);
        [self.tableView.mj_header endRefreshing];
    }];
}

#pragma mark - 数据源与代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellId = @"cell";
    YCRecruitCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        
        cell = [[YCRecruitCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId DataSource:self.dataArray[indexPath.row]];
    }
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

@end
